# --*-- coding:utf-8 --*--

import warnings
warnings.filterwarnings("ignore")
import requests
import datetime
import time
import math
from multiprocessing import Pool
from multiprocessing.dummy import Pool
import random




class Shop_flow_analysis():

	map_and_table = {} #装图跟表的数据容器

	cookies = None
	head = {
			'Host': 'sycm.taobao.com',
			'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'

	}
	dateRange = None

	def dateRanges(self,beginDate, endDate):
			dates = []
			dt = datetime.datetime.strptime(beginDate, '%Y-%m-%d')
			date = beginDate[:]
			while date <= endDate:
				dates.append(date)
				dt = dt + datetime.timedelta(1)
				date = dt.strftime('%Y-%m-%d')
			return dates


	# def store_flow_analysis(self):

	# 	#店铺流量分析30天总数据

	# 	store_flow_dict = {}  #装30天总数据

	# 	dateRange = self.dateRange['start'] + "|"+ self.dateRange['end']

	# 	url = 'https://sycm.taobao.com/flow/v3/overview/shopFlowSourceTop.json'

	# 	data = {
	# 			'dateRange':dateRange,
	# 			'dateType':'recent30',
	# 			'pageSize':'10',
	# 			'page':'1',
	# 			'order':'desc',
	# 			'orderBy':'uv',
	# 			'device':'2',   # 1 PC  2 WX
	# 			'belong':'all',
	# 			'indexCode':'uv,crtByrCnt,crtRate',
	# 			'_':'1536714358824',
	# 			'token':'1a998269c'
	# 	}

	# 	# 无线端数据
	# 	resp = requests.get(url,headers = self.head,cookies = self.cookies,verify=False,params = data)
	# 	store_flow_dict['WX'] = resp.json()

	# 	time.sleep(1)

	# 	# PC端数据
	# 	data['device'] = '1'
	# 	resp = requests.get(url,headers = self.head,cookies = self.cookies,verify=False,params = data)
	# 	store_flow_dict['PC'] = resp.json()
	# 	self.map_and_table['table'] = store_flow_dict  # 表格30填数据装入


	# def store_day_data(self,dateRa):

	# 	## 获取每天数据

	# 	dateRa = str(dateRa) + '|' + str(dateRa)

	# 	time.sleep(random.uniform(1,5))

	# 	store_day_data = []

	# 	url = 'https://sycm.taobao.com/flow/v3/overview/shopFlowSourceTop.json'

	# 	data = {
	# 			'dateRange':dateRa,
	# 			'dateType':'day',
	# 			'pageSize':'10',
	# 			'page':'1',
	# 			'order':'desc',
	# 			'orderBy':'uv',
	# 			'device':'2',   # 1 PC  2 WX
	# 			'belong':'all',
	# 			'indexCode':'uv,crtByrCnt,crtRate',
	# 			'_':'1536714358824',
	# 			'token':'1a998269c'
	# 	}

	# 	# 无线端数据
	# 	resp = requests.get(url,headers = self.head,cookies = self.cookies,verify=False,params = data)

	# 	# print(resp.text)  #{"code":5810,"msg":"You must login system first."}   {"traceId":"0b0ad71115397457459846320e0145","code":1003,"message":"请求参数非法"}
	# 	if resp.json()['code'] == 5810 or  resp.json()['code'] == 1003:
	# 		return resp.json()


	# 	else:
	# 		for k in resp.json()['data']:
	# 			# if k['pageName'] == '手淘搜索':
	# 			store_day_data.append({"WX":k})

	# 	# time.sleep(1)

	# 	# PC端数据
	# 	data['device'] = '1'
	# 	resp = requests.get(url,headers = self.head,cookies = self.cookies,verify=False,params = data)
	# 	# store_day_data['PC'] = resp.json()
	# 	for h in resp.json()['data']:
	# 		# if h['pageName'] == '手淘搜索':
	# 		store_day_data.append({"PC":h})

	# 	return store_day_data

	# def resp_all_data(self):

	# 	# 装入30天数数据
	# 	self.store_flow_analysis()  

	# 	# 装入每天数数据

	# 	time_list = self.dateRanges(self.dateRange['start'],self.dateRange['end'])
	# 	pool = Pool(5)
	# 	data = pool.map(self.store_day_data,time_list)
	# 	pool.close()
	# 	pool.join()
	# 	self.map_and_table['map'] = data
	# 	return self.map_and_table


	def FlowData(self,dateRa):

		# 店铺来源取数 	
		dateRa = str(dateRa) + '|' + str(dateRa)

		time.sleep(random.uniform(1,10))

		url = 'https://sycm.taobao.com/flow/v3/shop/source/tree.json'
		data = {
			'dateRange':dateRa,
			'dateType':'day',
			'pageSize':'10',
			'page':'1',
			'order':'desc',
			'orderBy':'uv',
			'device':'2',#无线端
			'belong':'all',
			'indexCode':'uv,crtByrCnt,crtRate,payByrCnt,payPct',
			'_':'1540535770157',
			'token':'fedc98d5e'
		}

		# 无线
		resp = requests.get(url,headers = self.head,cookies = self.cookies,verify=False,params = data)
		wx_ten_top = []  #无线店铺流量排名前10
		if resp.json()['code'] == 5810 or  resp.json()['code'] == 1003:
			return resp.json()
		else:
			for day_flow in resp.json()['data']:
				try:
					for any_item in day_flow['children']:
						wx_ten_top.append(any_item)
				except:
					pass
			
			# 有线
			data['device'] = '1'
			resp = requests.get(url,headers = self.head,cookies = self.cookies,verify=False,params = data)
			pc_ten_top = []  #无线店铺流量排名前10
			for day_flow in resp.json()['data']:
				try:
					for any_item in day_flow['children']:
						pc_ten_top.append(any_item)
				except:
					pass

			return {'wx':wx_ten_top,'pc':pc_ten_top}


	def Any_day_data(self,dates):

		# 接受时间区间返回时间区间数据

		time_list = self.dateRanges(dates['start'],dates['end'])
		pool = Pool(math.ceil(len(time_list)/5)) #总天数/5
		data = pool.map(self.FlowData,time_list)
		pool.close()
		pool.join()

		try:
			data[0]['code']
			return data  # 取不到数据返回
		except:
			# 数据处理
			shop_data = {}
			wx_anay_day_data = []
			for i in data:
				for k in i['wx']:
					if k['statDate']['value'] == 1410796800000:  # 去掉页面看不到的数据
						pass
					else:
						wx_anay_day_data.append({"pageName":k["pageName"]['value'],"uv":k["uv"]['value'],"crtByrCnt":k['crtByrCnt']['value'],"crtRate":k['crtRate']['value'],"payPct":round( k['payPct']['value'] , 2 ),"payByrCnt":k['payByrCnt']['value'],"statDate":k['statDate']['value']})
			shop_data['wx'] = wx_anay_day_data

			pc_anay_day_data = []
			for i in data:
				for k in i['pc']:
					if k['statDate']['value'] == 1410796800000:
						pass
					else:
						pc_anay_day_data.append({"pageName":k["pageName"]['value'],"uv":k["uv"]['value'],"crtByrCnt":k['crtByrCnt']['value'],"crtRate":k['crtRate']['value'],"payPct":round( k['payPct']['value'] , 2 ),"payByrCnt":k['payByrCnt']['value'],"statDate":k['statDate']['value']})
			shop_data['pc'] = pc_anay_day_data

			return shop_data




if __name__ == '__main__':
	
	Shop_flow_analysis = Shop_flow_analysis()
	Shop_flow_analysis.dateRange = {"start":"2018-10-01","end":"2018-10-02"}

	#过期
	# Shop_flow_analysis.cookies = {"lgc":"%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97","t":"4a8b6b38fefc230faea967bfe82a5d97","unb":"3397628220","uc3":"vt3=F8dByRjNVHF0dVuWD%2B0%3D&id2=UNN0nnQ5PzBj6g%3D%3D&nk2=sHmGuBnK6YrEuQ%3D%3D&lg2=UtASsssmOIJ0bQ%3D%3D","isg":"BAoK5WRlZZ8lTekzUTcdoJW5V_ms-45VtXD0zpRDtt3oR6oBfIveZVA1Uz2brAbt","tracknick":"%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97","v":"0","csg":"56c8b942","cookie17":"UNN0nnQ5PzBj6g%3D%3D","uc1":"cookie16=UIHiLt3xCS3yM2h4eKHS9lpEOw%3D%3D&tmb=1&cookie21=V32FPkk%2FhSt4&cookie15=UtASsssmOIJ0bQ%3D%3D&existShop=true&pas=0&cookie14=UoTYN4HMWEewPg%3D%3D&tag=8&lng=zh_CN","JSESSIONID":"F49D269A84279C48CBF70232FAEE584B","cookie2":"17e1130afb4cc4a5fb989f6d56a62a80","_l_g_":"Ug%3D%3D","_cc_":"V32FPkk%2Fhw%3D%3D","sg":"%E5%BA%9702","dnk":"%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97","tg":"0","existShop":"MTU0MTA1MTgwOQ%3D%3D","mt":"np=","cookie1":"UNaOmjEdkzPF0d%2BKqRdPBjspDZcJ%2BrbnVjjQlzW63YA%3D","cna":"cJFTFKDA9n4CAcFwhT5/XTa9","skt":"a824e06e752b10e2","_tb_token_":"e4d103b83baf8","_nk_":"%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97"}

	#未过期
	Shop_flow_analysis.cookies = {
        "lgc": "%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97",
        "t": "45d40492acf2004a2daf50bd30d1eb2f",
        "unb": "3397628220",
        "uc3": "vt3=F8dByRjNVbPDB5zI6zY%3D&id2=UNN0nnQ5PzBj6g%3D%3D&nk2=sHmGuBnK6YrEuQ%3D%3D&lg2=UtASsssmOIJ0bQ%3D%3D",
        "isg": "BKqqBY1Ghb-VCgnToH5mVZKl9xlMGy51FZBULjRjVv2IZ0ohHKt-hfAV8x37jKYN",
        "tracknick": "%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97",
        "v": "0",
        "csg": "55d30ab8",
        "cookie17": "UNN0nnQ5PzBj6g%3D%3D",
        "uc1": "cookie16=Vq8l%2BKCLySLZMFWHxqs8fwqnEw%3D%3D&tmb=1&cookie21=V32FPkk%2FhSt4&cookie15=UIHiLt3xD8xYTw%3D%3D&existShop=true&pas=0&cookie14=UoTYN4HPVNfjpg%3D%3D&tag=8&lng=zh_CN",
        "JSESSIONID": "48DE1FF40DD9BCAA9B6923AAE0CD99E2",
        "cookie2": "19e81878a4653d35872c2612fd0999f3",
        "_l_g_": "Ug%3D%3D",
        "_cc_": "VFC%2FuZ9ajQ%3D%3D",
        "sg": "%E5%BA%9702",
        "dnk": "%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97",
        "tg": "0",
        "existShop": "MTU0MTA2MzkzOQ%3D%3D",
        "mt": "np=",
        "cookie1": "UNaOmjEdkzPF0d%2BKqRdPBjspDZcJ%2BrbnVjjQlzW63YA%3D",
        "cna": "cJFTFKDA9n4CAcFwhT5/XTa9",
        "skt": "092db9d69c60e29d",
        "_tb_token_": "fb3e317134ee3",
        "_nk_": "%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97"
    }
	# print (Shop_flow_analysis.resp_all_data())

	# 任意天
	print (Shop_flow_analysis.Any_day_data(Shop_flow_analysis.dateRange))